package ph.loong.deliveryman.utils;

import android.content.Context;
import android.content.SharedPreferences;

import ph.loong.deliveryman.app.MyApplication;

/**
 * Author jc.lu.
 * Dates 2018/2/22.
 * Email Tbsupland@163.com.
 * desc
 */

public class SPUtil {
    /**
     * 获取String类型的值
     * @param key
     * @return
     */
    public static String getString( String key) {
        SharedPreferences sp = MyApplication.Companion.getContext().getSharedPreferences("config", Context.MODE_PRIVATE);
        return sp.getString(key, "");
    }

    /**
     * 获取Int类型的值
     * @param key
     * @return
     */
    public static int getInt(String key) {
        SharedPreferences sp = MyApplication.Companion.getContext().getSharedPreferences("config", Context.MODE_PRIVATE);
        return sp.getInt(key, 0);
    }

    /**
     * 获取Boolean类型的值
     * @param key
     * @return
     */
    public static boolean getBoolean(String key) {
        SharedPreferences sp = MyApplication.Companion.getContext().getSharedPreferences("config", Context.MODE_PRIVATE);
        return sp.getBoolean(key, false);
    }

    /**
     * 万能的put方法  可以存储String/Int/Boolean类型的值
     * @param key
     * @param value
     */
    public static void put(String key, Object value) {
        SharedPreferences sp = MyApplication.Companion.getContext().getSharedPreferences("config", Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sp.edit();
        if (value instanceof String) {
            edit.putString(key, (String) value);
        } else if (value instanceof Integer) {
            edit.putInt(key, (int) value);
        } else if (value instanceof Boolean) {
            edit.putBoolean(key, (boolean) value);
        }
        edit.commit();
    }
}
